package util.stack;

import util.linkedList.LinkedList;
import util.linkedList.impl.SingleLinkedList;

/**
 * 链栈
 * @author Huzz
 * @created 2021-10-15 11:38
 * @param <T>
 */
public class LinkedStack<T> {

    private int count;

    private LinkedList<T> link;

    public LinkedStack() {
        link  = new SingleLinkedList<T>();
    }



    /**入栈
     *
     * @param val
     */
    public void push(T val){
        link.insertFirst(val);
        count++;
    }

    /**
     * 查找栈顶元素
     * @return
     */
    public T peek(){
        return link.getFirst();
    }

    /**
     * 出栈
     * @return
     */
    public T pop(){
        T result = link.getFirst();
        link.deleteFirst();
        return result;
    }

    /**
     * 栈的长度
     * @return
     */
    public int size(){
        return count;
    }

    /**
     * 是否为空
     * @return
     */
    public boolean isEmpty(){
        return count == 0;
    }

    /**
     * 打印栈
     */
    public void printArrayStack(){
        if (count == 0) {
            System.out.println("The Stack is Empty.");
            return;
        }
        StringBuilder stringBuilder = new StringBuilder("[");
        for (int i = 0; i < count; i++) {
            stringBuilder.append(link.get(i) + ", ");
        }
        stringBuilder.replace(stringBuilder.lastIndexOf(", "), stringBuilder.length(), "]");
        System.out.println(stringBuilder);
    }
}
